package com.xaicode.template.gendata.common;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Locker xaicode@sina.com
 * @version 1.0
 * @date 2022/5/24 0024 20:23
 */
@Slf4j
public class ParamUtil {

    public static Integer checkRatioNum(String rate) {
        try {
            int rateNum = Integer.parseInt(rate);
            if (rateNum < 0 || rateNum > 100) {
                throw new RuntimeException("输入的比率必须为 0-100");
            }
            return rateNum;
        } catch (Exception e) {
            throw new RuntimeException("输入的比率必须为 0-100");
        }
    }

    public static Date checkDate(String dateString) {
        //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            String timeString = timeFormat.format(new Date());
            String datetimeString = dateString + " " + timeString;
            Date date = datetimeFormat.parse(datetimeString);
            return date;
        } catch (ParseException e) {
            throw new RuntimeException("日期格式为 2020-02-02");
        }
    }


    public static Boolean checkBoolean(String bool) {
        if (bool.equals("1") || bool.equals("true"))
            return Boolean.TRUE;
        if (bool.equals("0") || bool.equals("false")) {
            return Boolean.FALSE;
        }
        throw new RuntimeException("是非类型参数请填写：1或0，对应true或false");
    }


    public static Integer[] checkRate(String rateString, int rateCount) {
        try {
            String[] rateArray = rateString.split(":");
            if (rateArray.length != rateCount) {
                throw new RuntimeException("比例个数不足");
            }
            Integer[] rateNumArr = new Integer[rateArray.length];
            for (int i = 0; i < rateArray.length; i++) {
                Integer rate = checkRatioNum(rateArray[i]);
                rateNumArr[i] = rate;
            }
            return rateNumArr;
        } catch (Exception e) {
            throw new RuntimeException("请参照类型 75:10:15");
        }
    }


    public static String[] checkArray(String str) {
        if (str == null) {
            throw new RuntimeException("搜索词为空");
        }

        String[] split = str.split(",");
        return split;
    }


    public static Integer checkCount(String count) {
        try {
            if (count == null) {
                return 0;
            }
            Integer rateNum = Integer.valueOf(count.trim());
            return rateNum;
        } catch (Exception e) {
            throw new RuntimeException("输入的数据必须为数字");
        }
    }

}
